package com.lbw.proxy.dynamicproxy;

import com.lbw.proxy.staticproxy.ITeacherDao;

import java.lang.reflect.Proxy;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/6/13 14:53
 */
public class ProxyFactory {
    private ITeacherDao iTeacherDao;

    public ProxyFactory(ITeacherDao iTeacherDao) {
        this.iTeacherDao = iTeacherDao;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(iTeacherDao.getClass().getClassLoader(),
                iTeacherDao.getClass().getInterfaces(),
                (proxy, method, args) -> {
                    System.out.println("代理开始");
                    Object invoke = method.invoke(iTeacherDao, args);
                    System.out.println("代理结束");
                    return invoke;
                });
    }
}
